import type { Router } from 'vue-router';
import NProgress from 'nprogress'; // progress bar
import 'nprogress/css/nprogress.css'; // 进度条样式
import createPermissionGuard from './permissionGuard';
// 仓库数据
import { useStoreApp } from '@/store';
// Don't change the order of creation
export function setupRouterGuard(router: Router) {
  createPermissionGuard(router);
  addPageGuard(router);
  createProgressGuard(router);
}

/**
 * Hooks for handling page state
 */
function addPageGuard(router: Router) {
  const storeApp = useStoreApp();
  router.beforeEach(async () => {
    storeApp.addBreadcrumbTool({ desc: '', btns: [] });
    return true;
  });
}

// Used to handle page loading status
function createProgressGuard(router: Router) {
  router.beforeEach(async () => {
    NProgress.start(); // start progress bar
    return true;
  });
  router.afterEach(async () => {
    NProgress.done(); // finish progress bar
    return true;
  });
}
